# Copyright (c) 2019 Presto Labs Pte. Ltd.
# Author: taekwon

import logging

from coin.exchange.base.order_gateway import OrderType, OrderSide
from coin.exchange.deribit_v1.kr_rest.futures_product import DeribitFuturesProduct
from coin.exchange.deribit_v2.order_gateway import DeribitFuturesOrderGateway
from coin.strategy.executor.aggressive.aggressive_executor import AggressiveExecutor
from coin.strategy.executor.passive.simple_mm_executor import SimpleMMExecutor
from coin.strategy.executor.util.traits import (order_logger, FuturesExecutorTraits)


class DeribitFuturesSimpleTraits(FuturesExecutorTraits):
  BuyOrderSide = [OrderSide.BUY]
  SellOrderSide = [OrderSide.SELL]

  def __init__(self, executor_config, order_gateway, logger=None):
    logger = logger or logging.getLogger('DeribitFuturesSimpleMMExecutor')
    super().__init__(executor_config, order_gateway, logger)

  def _get_position(self, product):
    return self.og.get_position(product)

  def _submit_order_impl(self, product, price, qty, order_side, post_only=False):
    if not self.og.submittable():
      self._logger.warn('Skip submission: %s %f@%f' % (product, qty, price))
      return False
    self.og.submit(product, price, qty, order_side, OrderType.LIMIT, post_only=post_only)

  @order_logger
  def _submit_order(self, product, price, qty, order_side):
    self._submit_order_impl(product, price, qty, order_side)

  @order_logger
  def _submit_pass_order(self, product, price, qty, order_side):
    self._submit_order_impl(product, price, qty, order_side, post_only=self.config.post_only)

  # TODO(taekwon): add amend method.
  # def _amend_order(self, order_id, product, price, qty, order_side):
  #   self.logger.info('Amend order(%s): %s %s %s @ %s', order_id, order_side.name, product, qty, price)
  #   self.og.amend(order_id, price, qty)

  def _cancel_orders_impl(self, order_id_list):
    self.og.cancel_multiple(order_id_list)


class DeribitFuturesSimpleMMExecutor(SimpleMMExecutor):
  ProductType = DeribitFuturesProduct
  OGType = DeribitFuturesOrderGateway
  Traits = DeribitFuturesSimpleTraits


class DeribitFuturesSimpleAggExecutor(AggressiveExecutor):
  ProductType = DeribitFuturesProduct
  OGType = DeribitFuturesOrderGateway
  Traits = DeribitFuturesSimpleTraits
